package com.batchengine.system;

import com.batchengine.Definitions;

import java.util.LinkedList;

/**
 * Created by sunil.incubator@yahoo.com.
 * User: sunil.kalva
 * Date: Aug 13, 2009
 */
public class SystemJobs {

    private static final SystemJobs systemJobFeeder = new SystemJobs();

    private LinkedList<WorkerTask> stopPoolQ = new LinkedList<WorkerTask>();

    private SystemJobs() {
    }

    public static SystemJobs getSystemJobFeeder() {
        return systemJobFeeder;
    }

    public synchronized void submitJob(WorkerTask workerTask) {
        stopPoolQ.addLast(workerTask);
    }

    public synchronized WorkerTask getNextPoolForStop() {

        try {
            return stopPoolQ.removeFirst();
        } catch (Exception e) {
            return null;
        }
    }

    public static class WorkerTask {

        private Long managerId;
        private Long poolId;
        private Long workerId;

        private Definitions.OperationType operationType;

        public WorkerTask(Long managerId, Definitions.OperationType operationType) {
            this.managerId = managerId;
            this.operationType = operationType;
        }

        public WorkerTask(Long managerId, Long poolId, Definitions.OperationType operationType) {
            this.managerId = managerId;
            this.poolId = poolId;
            this.operationType = operationType;
        }

        public WorkerTask(Long managerId, Long poolId, Long workerId, Definitions.OperationType operationType) {
            this.managerId = managerId;
            this.poolId = poolId;
            this.workerId = workerId;
            this.operationType = operationType;
        }

        public Definitions.OperationType getOperationType() {
            return operationType;
        }

        public void setOperationType(Definitions.OperationType operationType) {
            this.operationType = operationType;
        }

        public Long getManagerId() {
            return managerId;
        }

        public void setManagerId(Long managerId) {
            this.managerId = managerId;
        }

        public Long getPoolId() {
            return poolId;
        }

        public void setPoolId(Long poolId) {
            this.poolId = poolId;
        }

        public Long getWorkerId() {
            return workerId;
        }

        public void setWorkerId(Long workerId) {
            this.workerId = workerId;
        }

        @Override
        public String toString() {
            return "WorkerTask{" +
                    "managerId=" + managerId +
                    ", poolId=" + poolId +
                    ", workerId=" + workerId +
                    ", operationType=" + operationType +
                    '}';
        }
    }
}
